package com.mdc.mansion.core.modules.dishes.impl;

import static com.mdc.mansion.dataaccess.common.context.ContextHolder.currentRestaurant;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.minlog.Log;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.restaurant.RestaurantCommon;
import com.mdc.mansion.core.common.translate.TranslateLanguage;
import com.mdc.mansion.core.common.translate.TranslateService;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.basedata.RestaurantStaffReceiveDishesService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesFlavorService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesStandardService;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesDTO;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesStandardDTO;
import com.mdc.mansion.core.modules.order.RestaurantOrderItemService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.common.service.CrudService;
import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesComboDao;
import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesComboItemDao;
import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesDao;
import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesStandardDao;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesCombo;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesComboItem;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesFlavor;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesStandard;
import com.mdc.mansion.dataaccess.modules.dishes.vo.RestaurantDishesVo;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;


/**
 * 餐厅菜品信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantDishesServiceImpl extends CrudService<RestaurantDishesDao, RestaurantDishes>
		implements RestaurantDishesService {

	@Autowired
	private RestaurantDishesDao restaurantDishesDao;

	@Autowired
	private RestaurantDishesComboDao restaurantDishesComboDao;

	@Autowired
	private RestaurantDishesStandardDao restaurantDishesStandardDao;

	@Autowired
	private RestaurantDishesComboItemDao restaurantDishesComboItemDao;

	@Autowired
    private TranslateService translateService;

	@Autowired
	private RestaurantDishesStandardService restaurantDishesStandardService;

	@Autowired
	private RestaurantDishesFlavorService restaurantDishesFlavorService;

	@Autowired
	private RestaurantOrderItemService restaurantOrderItemService;
	
    @Autowired
    private RestaurantStaffReceiveDishesService restaurantStaffReceiveDishesService;


	/**
	 * 所有餐厅默认套餐分类ID
	 */
	private static String  categoryComboId = "tc001";

	/**
	 * 套餐菜品类型
	 */
	private static String dishesTypeCombo = "1";

	/**
	 * 普通菜品类型
	 */
	private static String dishesTypeDis = "0";

    @Override
    public List<RestaurantDishes> getListByIds(List<String> ids) {
        List<RestaurantDishes> restaurantDishesList= Lists.newArrayList();
        for(String id:ids){
            RestaurantDishes restaurantDishes = this.get(id);
            if(restaurantDishes!=null){
                restaurantDishesList.add(restaurantDishes);
            }
        }
        return restaurantDishesList;
    }

	/**
	 * 2018年1月3日 19:47:30 添加 xizf 针对前台 页面进行数据查询
	 * 手工实现分页功能
	 * 数据组装分成两部分: 1、查询及展示数据 2、分页参数
	 * 分页传参为: page Map
	 */
	@Override
	public Page<RestaurantDishes> findDishesInfoPage(RestaurantDishesVo restaurantDishesVo) throws Exception {

		Map<String, Object> parameter = new HashMap<String, Object>(256);
		List<Map<String, String>> resListMap = null;
		Page<RestaurantDishesVo> page = new Page<RestaurantDishesVo>();


		parameter.put("page", page);

//		restaurantDishesVo.setPage(page);

		if (dishesTypeDis.equals(restaurantDishesVo.getDishesType())) {
			/// 普通菜品
			resListMap = restaurantDishesDao.findDishesInfo(parameter);
			if (null == resListMap || 0 == resListMap.size()) {
				return null;
			}

		} else if (dishesTypeCombo.equals(restaurantDishesVo.getDishesType())) {
			// 套餐

			resListMap = restaurantDishesDao.findComboDishesInfo(parameter);
			if (null == resListMap || 0 == resListMap.size()) {
				return null;
			}

		}

		return null;

	}

	/**
	 * 可针对单个品类，全部菜品品类进行菜品详情的查询
	 * 2018年1月5日 23:03:51
	 */
	@Override
	public Map<String,Object> findDishesList(RestaurantDishesVo restaurantDishesVo) throws BusinessException,Exception{

		Map<String, Object> restaurantDisMap = new HashMap<String, Object>(256);
		List<RestaurantDishes> categoryList = null;
		List<RestaurantDishesCombo> comboList = null;
		List<RestaurantDishesVo> comboListTmp = null;
		List<RestaurantDishesVo> restaurantDisList = null;
		RestaurantDishesVo restaurantDishesVoTmp = null;
		RestaurantDishes restaurantDishes = null;
		RestaurantDishesCombo restaurantDishesCombo = null;

		try{

			if (StringUtils.isBlank(restaurantDishesVo.getRestaurantId())) {
				/**餐厅未登录,登录异常**/
				throw new  BusinessException(I18nUtil.getMessage("login.data.exception"));
			}
			restaurantDishes = new RestaurantDishes();
			restaurantDishesCombo = new RestaurantDishesCombo();
			restaurantDishes.setRestaurantId(restaurantDishesVo.getRestaurantId());
			restaurantDishesCombo.setRestaurantId(restaurantDishesVo.getRestaurantId());

			if (StringUtils.isBlank(restaurantDishesVo.getRestaurantDishesCategoryId())) {
				/**查询全量菜单**/
				categoryList = restaurantDishesDao.findList(restaurantDishes);
				comboList = restaurantDishesComboDao.findList(restaurantDishesCombo);

			} else if (restaurantDishesVo.getRestaurantDishesCategoryId().equals(categoryComboId))
			{
				/** 查询套餐菜单**/
//				restaurantDishesCombo.setId(restaurantDishesVo.getRestaurantDishesCategoryId());
				comboList = restaurantDishesComboDao.findList(restaurantDishesCombo);

			} else {
				/**查询其他菜品菜单**/
				restaurantDishes.setRestaurantDishesCategoryId(restaurantDishesVo.getRestaurantDishesCategoryId());
				categoryList = restaurantDishesDao.findList(restaurantDishes);

			}

			  /**
			   *  套餐信息不为空则补充替换菜品
			   *  把套餐下面菜品的信息获取，并再次获取替代菜品的信息
			   *  形成的格式是:List(套餐)-->LIST(菜品)-->List(替代菜品)
			   */
			if (! (null == comboList || 0 == comboList.size()) ){
				comboListTmp = new ArrayList<RestaurantDishesVo>();
				/** 对套餐进行查询和数据组装**/
				comboListTmp = this.getComboDisList( comboList, restaurantDishesVo);
				restaurantDisMap.put("comboList", comboListTmp);
			}


			/** 普通菜单查询结果不为空则补充规格信息**/
			if (! (null == categoryList || 0 == categoryList.size())) {
				restaurantDisList = new ArrayList<RestaurantDishesVo>();
				for (RestaurantDishes restaurantDishesTmp : categoryList) {
					restaurantDishesVoTmp = this.getComboItemDisList(restaurantDishesTmp,new RestaurantDishesVo());
					restaurantDisList.add(restaurantDishesVoTmp);
				}

				restaurantDisMap.put("dishesList", restaurantDisList);
			}
		}catch(BusinessException e)
		{
			Log.error(e.getMessage() );
			throw new BusinessException(e.getMessage());
		}catch(Exception e)
		{
			e.printStackTrace();
			/**未知异常,获取菜品清单失败**/
			throw new BusinessException(I18nUtil.getMessage("dishes.query.null.exception"));
		}


		return restaurantDisMap;
	}

	/**
	 * 对套餐进行查询及数据组装
	 * @param comboList
	 * @param restaurantDishesVo
	 * @return
	 */
	private List<RestaurantDishesVo> getComboDisList(List<RestaurantDishesCombo> comboList,
			RestaurantDishesVo restaurantDishesVo) throws BusinessException {

		RestaurantDishesVo restaurantDishesVotmp = null;
		List<RestaurantDishesVo> comboListTmp = new ArrayList<RestaurantDishesVo>();

		try{
			for (RestaurantDishesCombo restaurantDishesComboTmp : comboList) {

				restaurantDishesVotmp = new RestaurantDishesVo();
				BeanUtils.copyProperties(restaurantDishesVotmp, restaurantDishesComboTmp);

				//// 获取套餐下面包含的产品

				comboListTmp.add(this.getComboItemList(restaurantDishesVotmp));

			}
		}catch(BusinessException e)
		{
			Log.error(e.getMessage() );
			throw new BusinessException(e.getMessage());
		}catch(Exception e)
		{
			/**未知异常,获取套餐菜品失败**/
			throw new BusinessException(I18nUtil.getMessage("dishes.query.combo.exception"));
		}
		return comboListTmp;
	}

	/**
	 * 查询套餐包含的菜品信息
	 * @param restaurantDishesVo
	 * @return
	 */
	private RestaurantDishesVo getComboItemList(RestaurantDishesVo restaurantDishesVo) throws BusinessException {

		RestaurantDishesComboItem restaurantDishesComboItem = null;
		List<RestaurantDishesComboItem> comboItemList = null;
		List<RestaurantDishesVo> comboItemListTmp = new ArrayList<RestaurantDishesVo>();
		RestaurantDishesVo restaurantDishesVotmp = new RestaurantDishesVo();

		restaurantDishesComboItem = new RestaurantDishesComboItem();
		restaurantDishesComboItem.setRestaurantId(restaurantDishesVo.getRestaurantId());
		restaurantDishesComboItem.setRestaurantDishesComboId(restaurantDishesVo.getId());

		try{
			comboItemList = restaurantDishesComboItemDao.findList(restaurantDishesComboItem);

			if (!(null == comboItemList || 0 == comboItemList.size())) {
				for (RestaurantDishesComboItem restaurantDishesComboItemTmp : comboItemList) {
					/** 需对菜品进行查询，并处理可替换菜品的问题**/
					RestaurantDishes restaurantDishes = new RestaurantDishes();

					restaurantDishes.setId(restaurantDishesComboItemTmp.getRestaurantDishesId());
					restaurantDishes.setRestaurantId(restaurantDishesVo.getRestaurantId());

					restaurantDishes = restaurantDishesDao.findList(restaurantDishes).get(0);

					restaurantDishesVo.setSwitchDishes(restaurantDishesComboItemTmp.getSwitchDishes());

					restaurantDishesVotmp = this.getComboItemDisList(restaurantDishes,restaurantDishesVo);
					restaurantDishesVotmp.setId(restaurantDishesComboItemTmp.getRestaurantDishesId());
					restaurantDishesVotmp.setName(restaurantDishesComboItemTmp.getDishesNum());
					comboItemListTmp.add(restaurantDishesVotmp);

				}

				}
			}catch(BusinessException e)
			{
				Log.error(e.getMessage() );
				throw new BusinessException(e.getMessage());
			}catch(Exception e)
			{
				/**未知异常,获取套餐子菜品失败**/
				throw new BusinessException(I18nUtil.getMessage("dishes.query.combo.item.exception"));
			}
			restaurantDishesVo.setDishesList(comboItemListTmp);
			return restaurantDishesVo;
		}


	/**
	 *  组装菜品信息，并获取套餐替代菜品，菜品规格等
	 *  注意替代菜品的保存方式
	 *
	 * @param restaurantDishes
	 * @param restaurantDishesVo
	 */
	private RestaurantDishesVo getComboItemDisList(RestaurantDishes restaurantDishes,RestaurantDishesVo restaurantDishesVo)  throws BusinessException{
		RestaurantDishesVo restaurantDishesVotmp = null;

		List<RestaurantDishesStandard> standardList = null;
		List<RestaurantDishesStandard> flavorList = null;

			/**取套餐下面的菜品**/
		try
		{
				if (null != restaurantDishes) {
					restaurantDishesVotmp = new RestaurantDishesVo();
					BeanUtils.copyProperties(restaurantDishesVotmp, restaurantDishes);

					///对菜品的规格/口味进行查询
					restaurantDishesVotmp.setStandName(RestaurantCommon.RestaurantDishesStand);
					standardList = this.getStandList(restaurantDishesVotmp);
					if(! (null == standardList || 0 ==standardList.size()))
					{
						restaurantDishesVotmp.setDishesStandardList(standardList);
					}
					restaurantDishesVotmp.setStandName(RestaurantCommon.RestaurantDishesFlavor);
					flavorList = this.getStandList(restaurantDishesVotmp);
					if(! (null == flavorList || 0 ==flavorList.size()))
					{
						//restaurantDishesVotmp.setFlavorList(flavorList);
					}

					/** 可替代菜品不为空 ， 只有套餐菜品才会有替代菜品**/
					if (StringUtils.isNotBlank(restaurantDishesVo.getSwitchDishes())) {
						restaurantDishesVotmp.setComboItemList(this.getRestaurantDisList(restaurantDishesVo.getSwitchDishes(),restaurantDishes.getRestaurantId()));
					}


				}
			}catch(BusinessException e)
			{
				Log.error(e.getMessage());
				throw new BusinessException(e.getMessage());
			}catch(Exception e)
			{
				e.printStackTrace();
				/**未知异常,获取菜品规格失败**/
				throw new BusinessException(I18nUtil.getMessage("dishes.query.standard.exception"));
			}
			return restaurantDishesVotmp;

	}

	/**
	 * 菜品详情查询
	 * 注意 替代菜品的保存方式
	 * @param switchDishes
	 * @param restaurantId
	 * @return
	 */
	private List<RestaurantDishesVo> getRestaurantDisList(String switchDishes, String restaurantId) throws BusinessException {
		RestaurantDishes restaurantDishes = null;
		List<RestaurantDishesVo> returnDishesVoList = new ArrayList<RestaurantDishesVo>();

		String[] dishesArray = switchDishes.split(",");
		try{
			for (String dishes : dishesArray) {
				///菜品ID
				String dishesid = dishes.split(":")[0];
				//菜品数量
				String dishesNum = dishes.split(":")[1];

				restaurantDishes = new RestaurantDishes();
				restaurantDishes.setId(dishesid);
				restaurantDishes.setRestaurantId(restaurantId);
				restaurantDishes = restaurantDishesDao.findList(restaurantDishes)	.get(0);

				RestaurantDishesVo restaurantDishesVotmp2 = this.getComboItemDisList(restaurantDishes,null);
			    restaurantDishesVotmp2.setDishesNum(dishesNum);

				returnDishesVoList.add(restaurantDishesVotmp2);
			}
		}catch(BusinessException e)
		{
			Log.error(e.getMessage());
			throw new BusinessException(e.getMessage());
		}catch(Exception e)
		{
			/**未知异常,获取菜品信息失败**/
			throw new BusinessException(I18nUtil.getMessage("dishes.query.standard.exception"));
		}
		return returnDishesVoList;

	}

	/**
	 * 规格及口味查询数据处理
	 * @param restaurantDishesVotmp
	 * @return
	 */
	private List<RestaurantDishesStandard> getStandList(RestaurantDishesVo restaurantDishesVotmp)  throws BusinessException{
		RestaurantDishesStandard restaurantDishesStandard = null;
		List<RestaurantDishesStandard> standardList = null;
		try
		{
			restaurantDishesStandard = new RestaurantDishesStandard();
			restaurantDishesStandard.setRestaurantId(restaurantDishesVotmp.getRestaurantId());
			restaurantDishesStandard.setRestaurantDishesId(restaurantDishesVotmp.getId());
			restaurantDishesStandard.setEnglishName(restaurantDishesVotmp.getStandName());

			standardList = restaurantDishesStandardDao.findList(restaurantDishesStandard);
			if(null == standardList || 0 == standardList.size())
			{
				return null;
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			Log.error(e.getMessage());
			throw new BusinessException(I18nUtil.getMessage("dishes.query.info.exception"));
		}


		return standardList;
	}

	/**
	 * 2018年1月7日 11:34:36 调整菜单排序
	 */
	@Override
	@Transactional
	public void updateDisSort(String data) throws BusinessException {
		try {
			JSONObject json=JSONObject.parseObject(data);
			JSONArray dishesList =json.getJSONArray("dishesList");
			if(dishesList==null || dishesList.size()<=0){return;}
			for(int i=0;i<dishesList.size();i++){
				JSONObject job=dishesList.getJSONObject(i);
				RestaurantDishes restaurantDishes=new RestaurantDishes();
				restaurantDishes.setId(job.getString("id"));
				restaurantDishes.setSortNum(job.getInteger("sortNum"));
				restaurantDishes.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
				this.updateSelective(restaurantDishes);
			}
		}catch(Exception e) {
			/**更新菜单排序失败**/
			e.printStackTrace();
			Log.error(e.getMessage());
			throw new BusinessException(I18nUtil.getMessage("dishes.edit.sort.exception"));
		}
	}

	/**
	 * 菜品删除
	 * 首先删除规格和口味
	 * 如果菜品在某个套餐内，则提示无法删除
	 * 再删除菜品
	 * 事务管理
	 */
	@Override
	@Transactional
	public void deleteDishesAndStand(String restaurantDishesId) {
		Restaurant restaurant=ContextHolder.currentRestaurant();
		RestaurantDishesComboItem restaurantDishesComboItem = new RestaurantDishesComboItem();
		restaurantDishesComboItem.setRestaurantId(restaurant.getId());
		restaurantDishesComboItem.setRestaurantDishesId(restaurantDishesId);
		List<RestaurantDishesComboItem> itemList = restaurantDishesComboItemDao.findList(restaurantDishesComboItem);
		if(CollectionUtils.isNotEmpty(itemList)) {
			Log.error("该菜品存在于套餐中，不能删除，请先从 套餐删除本菜品");
			throw new BusinessException(I18nUtil.getMessage("dishes.delete.item.fail.exception"));
		}

		int count=restaurantOrderItemService.countNotFinalByDishesId(restaurantDishesId);
		if(count>0){
			throw new BusinessException(I18nUtil.getMessage("dishes.delete.item.fail.exception"));
		}
		RestaurantDishesStandard restaurantDishesStandard = new RestaurantDishesStandard();
		restaurantDishesStandard.setRestaurantId(restaurant.getId());
		restaurantDishesStandard.setRestaurantDishesId(restaurantDishesId);
		List<RestaurantDishesStandard> standList = restaurantDishesStandardDao.findList(restaurantDishesStandard);
		/**查询之后做删除操作**/
		if(CollectionUtils.isNotEmpty(standList)) {
			for(RestaurantDishesStandard restaurantDishesStandardTmp : standList){
				restaurantDishesStandardDao.delete(restaurantDishesStandardTmp);
			}
		}

		RestaurantDishesFlavor restaurantDishesFlavor=new RestaurantDishesFlavor();
		restaurantDishesFlavor.setRestaurantId(restaurant.getId());
		restaurantDishesFlavor.setRestaurantDishesId(restaurantDishesId);
		List<RestaurantDishesFlavor> restaurantDishesFlavors = restaurantDishesFlavorService.findList(restaurantDishesFlavor);
		if(CollectionUtils.isNotEmpty(restaurantDishesFlavors)){
			for(RestaurantDishesFlavor restaurantDishesFlavorTmp : restaurantDishesFlavors){
				restaurantDishesFlavorService.delete(restaurantDishesFlavorTmp);
			}
		}

		restaurantDishesStandard = new RestaurantDishesStandard();
		restaurantDishesStandard.setRestaurantId(restaurant.getId());
		restaurantDishesStandard.setRestaurantDishesId(restaurantDishesId);
		standList = restaurantDishesStandardDao.findList(restaurantDishesStandard);
		/**查询之后做删除操作**/
		if(CollectionUtils.isNotEmpty(standList)) {
			for(RestaurantDishesStandard restaurantDishesStandardTmp : standList){
				restaurantDishesStandardDao.delete(restaurantDishesStandardTmp);
			}
		}
		/**删除菜品***/
		RestaurantDishes restaurantDishes = new RestaurantDishes();
		restaurantDishes.setRestaurantId(restaurant.getId());
		restaurantDishes.setId(restaurantDishesId);
		restaurantDishesDao.delete(restaurantDishes);
	}

	/**
	 * 保存和更新菜品信息
	 * 包含更新菜品和规格/口味信息
	 * 先确定是新增还是更新
	 * 新增和更新均为先处理菜品信息，再处理规格信息
	 * 2018年2月7日 10:05:06 对菜品修改进行处理,逻辑为：
	 * 1、当菜品处于套餐中的时候，只能修改菜品照片
	 * 2、当菜品不在套餐中，用户点餐、在未制作、制作中、未结账的时候，不能修改
	 */
	@Transactional
	@Override
	public void saveDishes(RestaurantDishesDTO restaurantDishesDTO) throws BusinessException, InvocationTargetException, IllegalAccessException {
		restaurantDishesDTO.verfiy();
		String name=restaurantDishesDTO.getName();
		String recommend = restaurantDishesDTO.getRecommend();
		restaurantDishesDTO.setChineseName(translateService.translate(name, TranslateLanguage.CHINA));
		restaurantDishesDTO.setEnglishName(translateService.translate(name, TranslateLanguage.ENGLISH));
		restaurantDishesDTO.setName(translateService.translate(name, TranslateLanguage.INDONESIA));
		/****2018年3月11日 21:09:05 添加备注信息翻译****/
		restaurantDishesDTO.setChineseRecommend(translateService.translate(recommend, TranslateLanguage.CHINA));
		restaurantDishesDTO.setEnglishRecommend(translateService.translate(recommend, TranslateLanguage.ENGLISH));
		restaurantDishesDTO.setRecommend(translateService.translate(recommend, TranslateLanguage.INDONESIA));
		
		RestaurantDishes restaurantDishes = new RestaurantDishes();
		BeanUtils.copyProperties(restaurantDishes,restaurantDishesDTO);
		restaurantDishes.setRestaurantId(currentRestaurant().getId());
		restaurantDishes.setUpdateBy(RestaurantSupport.getCurrentOperationUser());

		if(StringUtils.isBlank(restaurantDishesDTO.getId())){// 新增
			restaurantDishes.setCreateBy(RestaurantSupport.getCurrentOperationUser());
			
			this.save(restaurantDishes);
			restaurantDishesDTO.setId(restaurantDishes.getId());
			///对厨师进行菜品接收处理
			try{
	            Map<String,String> paraMap = Maps.newHashMap();
	            paraMap.put("restaurantId", restaurantDishes.getRestaurantId());
	            paraMap.put("restaurantDishesId", restaurantDishes.getId());
	            paraMap.put("userId",restaurantDishes.getUpdateBy().getLoginName());
	            restaurantStaffReceiveDishesService.initializeStaffReceiveDishes(paraMap);
			}catch(Exception e)
			{
				Log.error("初始化厨房接收菜品失败:"+e.getMessage());
			}
		}else{// 修改
			RestaurantDishesComboItem restaurantDishesComboItem = new RestaurantDishesComboItem();
			restaurantDishesComboItem.setRestaurantId(currentRestaurant().getId());
			restaurantDishesComboItem.setRestaurantDishesId(restaurantDishesDTO.getId());
			List<RestaurantDishesComboItem> itemList = restaurantDishesComboItemDao.findList(restaurantDishesComboItem);
			/***查看是否存在于套餐中，存在于套餐中则只更新图片***/
			if(CollectionUtils.isNotEmpty(itemList)) {
				this.updateDishesInfo(restaurantDishesDTO);
			}
			this.updateSelective(restaurantDishes);
		}
		reAddRestaurantDishesStandard(restaurantDishesDTO, restaurantDishes);
		reAddRestaurantDishesFlavor(restaurantDishesDTO, restaurantDishes);
		
	}

	/****
	 * 套餐参数判断及报错反馈
	 * 先查询菜品对应对应的数据，和传入参数进行对比
	 * @param restaurantDishesDTO
	 */
	@Transactional
	protected void updateDishesInfo(RestaurantDishesDTO restaurantDishesDTO) {

		RestaurantDishes restaurantDishes = this.get(restaurantDishesDTO.getId());

		if( null !=restaurantDishes )
		{
			////名称、价格、优惠价格只要有一个不同则提示不能修改
			if(!(restaurantDishes.getName().equals(restaurantDishes.getName()) && restaurantDishes.getPrice().equals(restaurantDishes.getPrice())
					&&restaurantDishes.getDiscountPrice().equals(restaurantDishes.getDiscountPrice()) &&restaurantDishes.getKitchenOfferFlag() ==restaurantDishesDTO.getKitchenOfferFlag() ))
			{

				/***套餐中的菜品不允许修改名称, 价格,优惠价格,规格,口味等信息***/
				throw new BusinessException(I18nUtil.getMessage("dishes.edit.info.error.exception"));

			}

			////只能修改介绍,特价,新品,图片信息
			restaurantDishes = new RestaurantDishes();
			restaurantDishes.setId(restaurantDishesDTO.getId());
			if( StringUtils.isNotBlank(""+restaurantDishesDTO.getSpecialPriceFlag()))
			{restaurantDishes.setSpecialPriceFlag(restaurantDishesDTO.getSpecialPriceFlag());}

			if( StringUtils.isNotBlank(""+restaurantDishesDTO.getNewOfferFlag()))
			{restaurantDishes.setNewOfferFlag(restaurantDishesDTO.getNewOfferFlag());}

			if( StringUtils.isNotBlank(restaurantDishesDTO.getBigUrl()))
			{restaurantDishes.setBigUrl(restaurantDishesDTO.getBigUrl());}

			this.updateSelective(restaurantDishes);

		}
	}

	@Transactional
	protected void reAddRestaurantDishesFlavor(RestaurantDishesDTO restaurantDishesDTO, RestaurantDishes restaurantDishes) {
		RestaurantDishesFlavor restaurantDishesFlavor = new RestaurantDishesFlavor();
		restaurantDishesFlavor.setRestaurantDishesId(restaurantDishes.getId());
		restaurantDishesFlavor.setRestaurantId(currentRestaurant().getId());
		List<RestaurantDishesFlavor> oldDishesFlavorList = restaurantDishesFlavorService.findList(restaurantDishesFlavor);
		if(oldDishesFlavorList!=null && oldDishesFlavorList.size()>0){
			for(RestaurantDishesFlavor restaurantDishesFlavorTmp : oldDishesFlavorList){
				restaurantDishesFlavorService.delete(restaurantDishesFlavorTmp);
			}
		}
		// 添加新的套口味
		if(CollectionUtils.isNotEmpty(restaurantDishesDTO.getDishesFlavorList())){
			for(RestaurantDishesFlavor restaurantDishesFlavorTmp : restaurantDishesDTO.getDishesFlavorList()){
				restaurantDishesFlavorTmp.setChineseName(translateService.translate(restaurantDishesFlavorTmp.getName(), TranslateLanguage.CHINA));
				restaurantDishesFlavorTmp.setEnglishName(translateService.translate(restaurantDishesFlavorTmp.getName(), TranslateLanguage.ENGLISH));
				restaurantDishesFlavorTmp.setName(translateService.translate(restaurantDishesFlavorTmp.getName(), TranslateLanguage.INDONESIA));
				restaurantDishesFlavorTmp.setRestaurantDishesId(restaurantDishes.getId());
				restaurantDishesFlavorTmp.setRestaurantId(currentRestaurant().getId());
				restaurantDishesFlavorTmp.setCreateBy(RestaurantSupport.getCurrentOperationUser());
				restaurantDishesFlavorTmp.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
				restaurantDishesFlavorService.save(restaurantDishesFlavorTmp);
			}
		}
	}

	@Transactional
	protected void reAddRestaurantDishesStandard(RestaurantDishesDTO restaurantDishesDTO, RestaurantDishes restaurantDishes) {
		RestaurantDishesStandard restaurantDishesStandard = new RestaurantDishesStandard();
		restaurantDishesStandard.setRestaurantDishesId(restaurantDishes.getId());
		restaurantDishesStandard.setRestaurantId(currentRestaurant().getId());
		List<RestaurantDishesStandard> oldDishesStandList = restaurantDishesStandardService.findList(restaurantDishesStandard);
		if(oldDishesStandList!=null && oldDishesStandList.size()>0){
			for(RestaurantDishesStandard restaurantDishesStandardTmp : oldDishesStandList){
				restaurantDishesStandardService.delete(restaurantDishesStandardTmp);
			}
		}
		// 添加新的套餐菜品
		if(CollectionUtils.isNotEmpty(restaurantDishesDTO.getDishesStandardList())){
			for(RestaurantDishesStandardDTO restaurantDishesStandardTmp : restaurantDishesDTO.getDishesStandardList()){
				restaurantDishesStandardTmp.setChineseName(translateService.translate(restaurantDishesStandardTmp.getName(), TranslateLanguage.CHINA));
				restaurantDishesStandardTmp.setEnglishName(translateService.translate(restaurantDishesStandardTmp.getName(), TranslateLanguage.ENGLISH));
				restaurantDishesStandardTmp.setName(translateService.translate(restaurantDishesStandardTmp.getName(), TranslateLanguage.INDONESIA));
				restaurantDishesStandardTmp.setRestaurantDishesId(restaurantDishes.getId());
				restaurantDishesStandardTmp.setRestaurantId(currentRestaurant().getId());
				restaurantDishesStandardTmp.setUpdateBy(RestaurantSupport.getCurrentOperationUser());
				restaurantDishesStandardTmp.setCreateBy(RestaurantSupport.getCurrentOperationUser());
				if(StringUtils.isNotBlank(restaurantDishesStandardTmp.getId())){
					restaurantDishesStandardService.delete(restaurantDishesStandardTmp);
				}
				restaurantDishesStandardService.save(restaurantDishesStandardTmp);
				if(restaurantDishesStandardTmp.getDefaultFlag()==1){
					restaurantDishes.setRestaurantDishesStandardId(restaurantDishesStandardTmp.getId());
					this.updateSelective(restaurantDishes);
				}
			}
		}
	}

    @Override
    public List<RestaurantDishesVo> findDishesListWithExt(RestaurantDishesVo restaurantDishesVo) {
    	List<RestaurantDishesVo> dishesList = restaurantDishesDao.getDishesList(restaurantDishesVo);
    	List<RestaurantDishesStandard> outStrandardList = null;
    	List<RestaurantDishesFlavor> outFlavorList = null;
    	
    	if(CollectionUtils.isNotEmpty(dishesList))
    	{
    		RestaurantDishesStandard dishesStandard = null;
    		String [] standardArray = null;
    		String [] standDetailArray = null;
    		for( RestaurantDishesVo detailVo : dishesList )
    		{
				outFlavorList = Lists.newArrayList();
				outStrandardList = Lists.newArrayList();
				
    			if(StringUtils.isNotBlank(detailVo.getRestaurantDishesStandardList()) )
    			{
    				standardArray = (detailVo.getRestaurantDishesStandardList()).split("\\|\\|");
    				for(String standard : standardArray)
    				{
    					dishesStandard = new RestaurantDishesStandard();
    					standDetailArray = standard.split("\\|");
    					if(6 != standDetailArray.length)
    					{
    						continue;
						}
    					dishesStandard.setId(standDetailArray[0]);
    					dishesStandard.setName(standDetailArray[1]);
    					dishesStandard.setChineseName(standDetailArray[2]);
    					dishesStandard.setEnglishName(standDetailArray[3]);
    					String price = StringUtils.isBlank(standDetailArray[4])?"0":standDetailArray[4];
    					dishesStandard.setPrice(new BigDecimal(price));
						String discountPrice = StringUtils.isBlank(standDetailArray[5])?"0":standDetailArray[5];
    					dishesStandard.setDiscountPrice(new BigDecimal(discountPrice));
    					
    					outStrandardList.add(dishesStandard);
    				}
    			}
    			
    			if(StringUtils.isNotBlank(detailVo.getRestaurantDishesFlavorList()))
    			{
    				RestaurantDishesFlavor dishesFlavor = null;
    				String [] flavorArray = null;
    	    		String [] flavorDetailArray = null;
    	    		
    	    		flavorArray = (detailVo.getRestaurantDishesFlavorList()).split("\\|\\|");
    				for(String flavorStr : flavorArray)
    				{
    					dishesFlavor = new RestaurantDishesFlavor();
    					flavorDetailArray = flavorStr.split("\\|");
						if(4 != flavorDetailArray.length)
						{
							continue;
						}
    					dishesFlavor.setId(flavorDetailArray[0]);
    					dishesFlavor.setName(flavorDetailArray[1]);
    					dishesFlavor.setChineseName(flavorDetailArray[2]);
    					dishesFlavor.setEnglishName(flavorDetailArray[3]);
    					outFlavorList.add(dishesFlavor);
    				}
    			}
    			detailVo.setDishesFlavorList(outFlavorList);
    			detailVo.setDishesStandardList(outStrandardList);
    		}
    	}else
    	{
    		return null;
    	}
    	
//        Page<RestaurantDishes> page = new Page<RestaurantDishes>(-1);
//		page.setOrderBy("sort_num");
//		RestaurantDishes queryRestaurantDishes=new RestaurantDishes();
//		org.springframework.beans.BeanUtils.copyProperties(restaurantDishesVo,queryRestaurantDishes);
//		queryRestaurantDishes.setPage(page);
//        List<RestaurantDishes> dishesList = this.findList(queryRestaurantDishes);
//        List<RestaurantDishesVo> dishesWithExtList = new ArrayList\\|\\|();
//        if(dishesList!=null && dishesList.size()>0){
//            for(RestaurantDishes restaurantDishes : dishesList){
//                dishesWithExtList.add(this.buildDishesExtends(restaurantDishes));
//            }
//        }
//        return dishesWithExtList;
        
        return dishesList;
    }

	@Override
	public List<RestaurantDishes> findCurrentRestaurantDishes() {
		RestaurantDishes dishes = new RestaurantDishes();
		dishes.setRestaurantId(currentRestaurant().getId());
		return this.findList(dishes);
	}

	private RestaurantDishesVo buildDishesExtends(RestaurantDishes restaurantDishes){
        RestaurantDishesVo restaurantDishesVo = new RestaurantDishesVo();
        org.springframework.beans.BeanUtils.copyProperties(restaurantDishes, restaurantDishesVo);
        String restaurantId = restaurantDishesVo.getRestaurantId();// 餐厅ID
        String restaurantDishesId = restaurantDishesVo.getId();// 菜品ID
        // 查询套餐规格列表
        RestaurantDishesStandard paramDishesStandard = new RestaurantDishesStandard();
        paramDishesStandard.setRestaurantId(restaurantId);
        paramDishesStandard.setRestaurantDishesId(restaurantDishesId);
        paramDishesStandard.setPage(new Page<RestaurantDishesStandard>(-1));
        List<RestaurantDishesStandard> dishesStandardList = restaurantDishesStandardService.findList(paramDishesStandard);
        // 查询套餐口味列表
        RestaurantDishesFlavor paramDishesFlavor = new RestaurantDishesFlavor();
        paramDishesFlavor.setRestaurantId(restaurantId);
        paramDishesFlavor.setRestaurantDishesId(restaurantDishesId);
        paramDishesFlavor.setPage(new Page<RestaurantDishesFlavor>(-1));
        List<RestaurantDishesFlavor> dishesFlavorList  =restaurantDishesFlavorService.findList(paramDishesFlavor);

        restaurantDishesVo.setDishesStandardList(dishesStandardList);
        restaurantDishesVo.setDishesFlavorList(dishesFlavorList);
        return restaurantDishesVo;
    }
}
